﻿using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace CMS
{
    internal static class NativeMethods
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        internal static extern int GetPrivateProfileInt(
            string lpAppName,
            string lpKeyName,
            int nDefault,
            string lpFileName
            );

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        internal static extern int GetPrivateProfileString(
            string lpAppName,
            string lpKeyName,
            string lpDefault,
            StringBuilder lpReturnedString,
            int nSize,
            string lpFileName
            );

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        internal static extern int WritePrivateProfileString(
            string lpAppName,
            string lpKeyName,
            string lpString,
            string lpFileName
            );
    }

    /// <summary>
    /// 标准INI读写类,此类请不要在游戏过程中使用,应使用PrivateProfile2代替,因为怀疑TP会监控此类使用到的相关接口,
    /// 此类的写入方式是立即写入的
    /// </summary>
    public class PrivateProfile 
    {
        public readonly FileInfo IniFile;

        /// <summary>
        /// 初始化一个标准INI读写器
        /// </summary>
        public PrivateProfile(string file) : this(new FileInfo(file))
        {
        }

        /// <summary>
        /// 初始化一个标准INI读写器
        /// </summary>
        public PrivateProfile(FileInfo fileInfo)
        {
            IniFile = fileInfo;
        }

        /// <summary>
        /// 读取某个值为整形
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public int ReadInt(string section, string key, int def)
        {
            return NativeMethods.GetPrivateProfileInt(section, key, def, IniFile.FullName);
        }

        /// <summary>
        /// 读取某个值为数字
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public string ReadString(string section, string key, string def)
        {
            StringBuilder vRetSb = new StringBuilder(2048);
            NativeMethods.GetPrivateProfileString(section, key, def, vRetSb, 2048, IniFile.FullName);
            return vRetSb.ToString();
        }

        /// <summary>
        /// 写入Int数值，如果不存在 节/键，则会自动创建
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="Ival">写入值</param>
        public void WriteInt(string section, string key, int Ival)
        {
            NativeMethods.WritePrivateProfileString(section, key, Ival.ToString(), IniFile.FullName);
        }

        /// <summary>
        /// 写入String字符串，如果不存在 节/键，则会自动创建
        /// </summary>
        /// <param name="section">节</param>
        /// <param name="key">键</param>
        /// <param name="strVal">写入值</param>
        public void WriteString(string section, string key, string strVal)
        {
            NativeMethods.WritePrivateProfileString(section, key, strVal, IniFile.FullName);
        }

        /// <summary>
        /// 删除指定的 节
        /// </summary>
        /// <param name="section"></param>
        public void DeleteSection(string section)
        {
            NativeMethods.WritePrivateProfileString(section, null, null, IniFile.FullName);
        }


        /// <summary>
        /// 读取指定 节/键 的值
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string ReadValue(string section, string key)
        {
            StringBuilder strSb = new StringBuilder(256);
            NativeMethods.GetPrivateProfileString(section, key, "", strSb, 256, IniFile.FullName);
            return strSb.ToString();
        }

        /// <summary>
        /// 写入指定值，如果不存在 节-键，则会自动创建
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void WriteValue(string section, string key, string value)
        {
            NativeMethods.WritePrivateProfileString(section, key, value, IniFile.FullName);
        }

        /// <summary>
        /// 删除所有的节
        /// </summary>
        public void ClearSection()
        {
            NativeMethods.WritePrivateProfileString(null, null, null, IniFile.FullName);
        }
    }
}
